package tr.edu.yildiz.moduleteacher.vaadin;


import tr.edu.yildiz.dynaView.service.Module;
import tr.edu.yildiz.dynaView.service.ModuleService;

import com.vaadin.ui.Button;
import com.vaadin.ui.Component;
import com.vaadin.ui.CustomComponent;
import com.vaadin.ui.Form;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Table;
import com.vaadin.ui.TextField;
import com.vaadin.ui.Button.ClickEvent;

public class ModuleTeacher implements Module<Component> {

	public static class ModuleComponent extends CustomComponent {

		public ModuleComponent() {
			Form studentForm = new Form();
			final TextField name = new TextField("Name");
			final TextField surname = new TextField("Surname");
			final TextField no = new TextField("No");
			studentForm.addField("Name",name );
			studentForm.addField("Surname", surname);
			HorizontalLayout footer = new HorizontalLayout();
			footer.setSpacing(true);
			Button save = new Button("Save");
			Button cancel = new Button("Cancel");
			footer.addComponent(save);
			footer.addComponent(cancel);
			studentForm.setFooter(footer);
			setCompositionRoot(studentForm);
			
			final Table table = new Table();
			
			table.addContainerProperty("Name", String.class, null);
			table.addContainerProperty("Surname", String.class, null);
			save.addListener(new Button.ClickListener() {
				
				public void buttonClick(ClickEvent event) {
					table.addItem(new Object[]{	name.getValue().toString(),surname.getValue().toString()},name.getValue().toString()+surname.getValue().toString());
					name.setValue("");
					surname.setValue("");
				}
			});
			
			cancel.addListener(new Button.ClickListener() {
				
				public void buttonClick(ClickEvent event) {
					name.setValue("");
					surname.setValue("");
				}
			});
			studentForm.addField("table", table);
			
			
		}
		
	}
	
	public String getName() {
		return "Teachers";
	}
	
	public Component createComponent() {
		return new ModuleComponent();
	}
	
	public void setModuleService(ModuleService service) {
		System.out.println("Teacher: registering with ModuleService");
		service.registerModule(this);
	}
	
	public void unsetModuleService(ModuleService service) {
		System.out.println("Teacher: unregistering with ModuleService");
		service.unregisterModule(this);
	}
}